﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MiniCrawler.Entity;

namespace MiniCrawler.DataAccess
{
    public class DataLinkHelper
    {
        public static DataLink GetLink(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }

        public static DataLink GetLink(CrwlDataEntities dbContext, string url)
        {
            var link = from s in dbContext.DataLinks where s.Value.Equals(url) select s;
            if (link != null && link.Count() > 0)
            {
                return link.First();
            }
            else
            {
                return null;
            }
        }
        public static DataLink GetLink(DataLink link)
        {
            if(link != null)
            {
                return GetLink(link.Value);
            }
            else
            {
                return null;
            }
        }
        
        public static bool IsLinkExist(string url)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where s.Value.Equals(url) select s;
            if(link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsLinkChecked(string url)
        {
            var link = GetLink(url);
            if(link != null)
            {
                return link.IsChecked;
            }
            return false;
        }

        public static List<DataLink> SelectUncheckedLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }

        public static List<DataLink> SelectUncheckedLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }

        public static List<DataLink> SelectUncheckedLinks(string website, int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.Website == website && s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }

        public static List<DataLink> SelectLinks()
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }

        public static List<DataLink> SelectLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.Level == level) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }

        public static List<DataLink> SelectLinks(string webSite, int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.Level == level && s.Website == webSite) select s;
            if (link != null && link.Count() > 0)
            {
                return link.ToList();
            }
            else
            {
                return new List<DataLink>();
            }
        }
        
        public static bool HasUncheckedLinks(int level)
        {
            var db = new CrwlDataEntities();
            var link = from s in db.DataLinks where (s.Level == level && s.IsChecked == false) select s;
            if (link != null && link.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int CreateLink(DataLink link)
        {
            var db = new CrwlDataEntities();
            db.DataLinks.Add(link);
            return db.SaveChanges();
        }

        public static int CreateLink(string value, string website, int level, DateTime dateCreated, bool isChecked, int dataExpressionId)
        {
            var link = new DataLink()
                           {
                               DateCreated = dateCreated,
                               IsChecked = isChecked,
                               Level = level,
                               Value = value,
                               Website = website,
                               DataExpressionId =  dataExpressionId
                           };
            return CreateLink(link);
        }

        public static int UpdateLink(DataLink link)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db , link.Value);
            currentLink.Level = link.Level;
            currentLink.IsChecked = link.IsChecked;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public static int UpdateLink(string url)
        {
            var db = new CrwlDataEntities();
            var currentLink = GetLink(db, url);
            currentLink.IsChecked = true;
            db.Entry(currentLink).State = EntityState.Modified;
            return db.SaveChanges();
        }

    }
}
